$(function () {

    /*加载权限*/

var cp =null;
    $.get("/permission/selectAll.do",function (data) {
        var ps=data




    var role_dig = $("#role_dig");
    var role_dg = $('#role_dg');
    var role_form = $('#role_form');
    var role_combotree =$("#role_combotree");
    var permissions = $('#permissions');
    var selectedPermission = $('#selectedPermission');

    /*给所有的a标签绑定点击事件*/

    var ObjectCmd ={
        /*保存*/
        saveUser:function () {
            role_form.form("clear")

            role_dig.dialog("setTitle", "add");
            role_dig.dialog("open");

        },
        /*编辑*/
        editUser: function editUser() {
            var tr = role_dg.datagrid("getSelected");

            if (!tr) {
                //没有选中
                $.messager.alert('温馨提示', "至少选中一条数据");
                return;
            }
            //设置角色菜单的回显
            $.get("/role/selectMenuByRoleId.do",{id:tr.id},function (ids) {
                role_combotree.combotree("setValues",ids)
            })
                //选中行,发送请求拿到对应的权限id
            var exps =permissions.datagrid("getRows");

            $.get("/role/permissionIds.do",{id:tr.id},function (ids) {

                //注意这个是减减 取出来每一个权限
                for(var i = exps.length - 1; i >= 0; i--) {
                    var permission = exps[i];

                    //判断是否存在 ids 数组中,如果存在就需要从所有权限中移除掉
                    var index = $.inArray(permission.id, ids);
                    if(index >= 0) {
                        selectedPermission.datagrid("appendRow",permission)
                        permissions.datagrid("deleteRow", i);
                    }
                }

            })
            role_form.form("clear")

            role_dig.dialog("setTitle", "editUser");
            role_dig.dialog("open");
            role_form.form("load", tr)
        },
        /*删除*/
        deleteUser: function () {
            var tr = $('#role_dg').datagrid("getSelected");
            if (!tr) {
                //没有选中
                $.messager.alert('温馨提示',"至少选中一条数据");
                return;
            }
            $.get("/role/delete.do",{id:tr.id},function (data) {
                if (data.success) {
                    $.messager.alert('温馨提示','删除成功','info',function () {


                        $('#role_dg').datagrid("reload");
                    });
                }
                else{
                    $.messager.alert('温馨提示',data.msg);


                }
            })
        },

        /*刷新*/
        reloadwin: function () {
            role_dg.datagrid("reload");
        },

        /*关闭对话框*/
        closeWin:function () {
            //清空选中权限
            selectedPermission.datagrid("loadData",[])
            //重新加载全部权限
            $.get("/permission/selectAll.do",function (data) {
                permissions.datagrid("loadData",data)
            })


            role_dig.dialog("close")

        },
        /*提交表单*/
        submitForm:function () {

            role_form.form({
                url: '/role/saveOrUpdate.do',
                onSubmit:function (param) {
                    var rows = selectedPermission.datagrid("getRows");
                    $.each(rows,function (i,ele) {
                        param["permissions["+i+"].id"] =ele.id;
                    })


                },
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {

                            ObjectCmd.closeWin();
                            role_dg.datagrid("reload");
                        });
                    }
                    else {
                        $.messager.alert('温馨提示', data.msg);
                        ObjectCmd.closeWin();

                    }
                }
            });

            role_form.submit();
        }
    }
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd")
        ObjectCmd[cmd]();

    })


                    //渲染菜单下拉框
                    role_combotree.combotree({
                        cascadeCheck:false,
                        url:'/menu/getMenusBySystem.do',
                        multiple:true,
                        onCheck:function (node) {
                            var treeObj = role_combotree.combotree('tree');
                            //获取父节点
                            var parent = treeObj.tree('getParent', node.target);
                            if (parent) {
                                console.log(parent.target);
                                treeObj.tree('check', parent.target);

                }
            }
        })
    //编辑对话框
    role_dig.dialog({
        width: 500,
        height: 500,
        closed: true,
        cache: false,
        modal: true,
        onClose:function () {
            
        },
        buttons: '#role_dig_btn'
    })
    //页面table
    role_dg.datagrid({
        url: '/role/query.do',
        fitColumns: true,
        fit: true,
        pagination: true,
        singleSelect: true,
        toolbar: "#role_tb",
        pageSize: 3,
        rownumbers: true,
        pageList: [3, 6, 9],

        columns: [[
            {field: 'id', title: '编号', width: 100},
            {field: 'name', title: '角色名称', width: 100},
            {field: 'sn', title: '角色编号', width: 100},


        ]]
    });

    //全部权限table
    permissions.datagrid({
        setTitle:'所有权限',
        data:ps,
        height:200,
        width:200,
        singleSelect: true,
        rownumbers:true,
        fitColumns:true,
        onClickRow:function (index, row) {
            selectedPermission.datagrid("appendRow",row)
            permissions.datagrid("deleteRow",index)

        },
        columns: [[

            {field: 'name', title: '权限名称', width: 100}

        ]]
    });
       cp = permissions.datagrid("getRows");

        //选中权限table
    selectedPermission.datagrid({
        setTitle:'选中权限',
        height:200,
        width:200,
        fitColumns:true,
        singleSelect: true,
        rownumbers:true,
        onClickRow:function (index, row) {
            permissions.datagrid("appendRow",row)
            selectedPermission.datagrid("deleteRow",index)

        },

        columns: [[
            {field: 'name', title: '权限名称', width: 100}


        ]]
    });
})})



